//
//  AddMeterImageParamCell.swift
//  Employee
//
//  Created by tancheng on 2018/6/21.
//  Copyright © 2018年 remotevillage. All rights reserved.
//

import UIKit
import Photos
class AddMeterImageParamCell: UITableViewCell {

    static let reuseIdentifier = "AddMeterImageParamCell"
    
    static let height = (UIScreen.width - 2 * 30 - 30) / 4 + 110
    static let imagePickViewHeight = (UIScreen.width - 2 * 30 - 30) / 4 + 10
    
    var addImgBlock:(([PHAsset])->())?
    var deleteImgBlock:((Int)->())?
    
    lazy var subContentView:UIView = {
        let vi = UIView()
        vi.backgroundColor = UIColor.white
        vi.layer.cornerRadius = 2
        vi.layer.masksToBounds = true
        return vi
    }()
    
    lazy var nameLab: UILabel = {
       let lab = UILabel()
        lab.font = GlobalConfig.system15Font
        lab.textColor = GlobalConfig.TextLightColor
        lab.text = "附件(选填)"
        return lab
    }()
    
    lazy var tipLab: UILabel = {
        let lab = UILabel()
        lab.font = GlobalConfig.system12Font
        lab.textColor = UIColor.hex("#323C49", alpha: 0.2)
        lab.text = "最多可以添加4个附件"
        return lab
    }()
    
    lazy var imagePickView: StaffSelectImageView = {
       let vi = StaffSelectImageView(frame: CGRect.zero)
        vi.selectedImg = {[weak self] selAssets in
            self?.paramModel?.images.removeAll()
            for item in selAssets {
                self?.paramModel?.images.append(item)
            }
            self?.addImgBlock?(selAssets)
        }
        vi.deleteImg = {[weak self] index in
            self?.paramModel?.images.remove(at: index)
            self?.deleteImgBlock?(index)
        }
       return vi
    }()
    
    fileprivate var selectImageV: SelectImagePopV!
    
    weak var targetVC: UIViewController? {
        didSet {
            imagePickView.targetVC = targetVC
        }
    }
    
    var paramModel: AddMeterModel? {
        didSet {
            
            imagePickView.assets = paramModel?.images ?? []
        }
    }
    
    
    //MARK:
    func setupUI() {
        
        contentView.backgroundColor = GlobalConfig.pageColor
        
        contentView.addSubview(subContentView)
        subContentView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(0)
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.bottom.equalToSuperview().offset(0)
        }
        
        subContentView.addSubview(nameLab)
        nameLab.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(20)
            make.top.equalToSuperview().offset(20)
        }
        
        subContentView.addSubview(imagePickView)
        imagePickView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-10)
            make.top.equalTo(nameLab.snp.bottom).offset(10)
            make.height.equalTo(AddMeterImageParamCell.imagePickViewHeight)
        }
        
        subContentView.addSubview(tipLab)
        tipLab.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(20)
            make.bottom.equalToSuperview().offset(-10)
        }
        
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
}
